//
//  RootViewController.m
//  IFengNews
//
//  Created by lanou3g on 16/3/2.
//  Copyright © 2016年 徐强. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()<UIGestureRecognizerDelegate>

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addTapGesture];
}
- (void)addTapGesture{
    if (!self.tap) {
        self.tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
        [self.tap setEnabled:NO];
        //代理，防止点击抽屉也会打开根视图
        self.tap.delegate = self;
        [self.view addGestureRecognizer:self.tap];
    }
}
- (void)tapAction{
    [self showRootViewController];
    //手势关闭
    [self.tap setEnabled:NO];
    //关闭用户交互
    self.rootVC.view.userInteractionEnabled = YES;
}
//设置根控制器
- (void)setRootViewController:(UIViewController *)viewController{
    //传进来的视图为空则返回，不设置根视图
    if (!viewController) {
        return;
    }
    /*
     分区管理一个view：在一个该view的控制器上添加不同的控制器，再通过对不同控制器的管理来实现分区管理view（注意：先添加控制器，后添加view）
     [self addChildViewController:a]
     [self.view addSubview:a.view];
     */
    if (self.rootVC) {
        [self.rootVC.view removeFromSuperview];
    }
    self.rootVC = viewController;
    [self addChildViewController:viewController];
    //如果手势开着，证明抽屉正开着
    if (self.tap.enabled) {
        viewController.view.frame = CGRectMake(kScreenWidth * 4.0 / 5, 0, kScreenWidth, kScreenHeight);
    }
    [self.view addSubview:viewController.view];
    //关闭抽屉
    [self showRootViewController];
    //设置导航栏左边打开抽屉的按钮
    [self setNaviButton];
}
//关闭抽屉
- (void)showRootViewController{
    [UIView animateWithDuration:0.4 animations:^{
        self.rootVC.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    }];
     //手势关闭,rootView可以使用
    [self.tap setEnabled:NO];
    self.rootVC.view.userInteractionEnabled = YES;
}
//打开抽屉
- (void)showLeftController{
    if (![self.view.subviews containsObject:self.leftVC.view]) {
        //插入抽屉的视图
        [self.view insertSubview:self.leftVC.view atIndex:0];
    }
    //打开抽屉时，根视图收成右侧部分界面
    [UIView animateWithDuration:0.4 animations:^{
        self.rootVC.view.frame = CGRectMake(kScreenWidth * 4.0 / 5, 0, kScreenWidth, kScreenHeight);
    }];
    //手势打开右侧界面
    [self.tap setEnabled:YES];
    self.rootVC.view.userInteractionEnabled = NO;
}
//设置打开抽屉的按钮
- (void)setNaviButton{
    UINavigationController *naviVC = (UINavigationController *)self.rootVC;
    UIViewController *viewController = naviVC.viewControllers[0];
    viewController.navigationItem.leftBarButtonItem = ({
        UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"分类" style:(UIBarButtonItemStyleDone) target:self action:@selector(openLeft)];
        item;
    });
}
- (void)openLeft{
    [self showLeftController];
    //手势打开
    [self.tap setEnabled:YES];
    //rootView禁止使用
    self.rootVC.view.userInteractionEnabled = NO;
}
//delegate：只在rootVC上点击才能关闭抽屉
- (BOOL )gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    if (gestureRecognizer == self.tap) {
        return CGRectContainsPoint(self.view.frame, [gestureRecognizer locationInView:self.rootVC.view]);
    }
    return YES;
}

@end
